package com.zlaidyrac.weride.ui;

import java.io.File;
import java.util.HashMap;

import com.zlaidyrac.weride.persistence.User;
import com.zlaidyrac.weride.webclient.WebClient;
import com.zlaidyrac.weride.main.Common;
import com.zlaidyrac.weride.main.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowPassengerActivity extends Activity {
	public static final String FB_FRIEND_IMAGE_PATH = "weride/friend_fb_img.png";
	public static final int BUDDY_BUTTON_MODE_CONFIRM = 1;
	public static final int BUDDY_BUTTON_MODE_CONFIRMED = 2;
	
	Button confirm_button;
	User passenger;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_passenger);
		Intent intent = getIntent();
		new GetPassengerAsyncTask().execute(intent.getStringExtra("user_id"));
		new DownloadFbImageAsyncTask().execute();
		int doneCount = 0;
		while ( doneCount == 0 ) {
			if ( passenger != null ) {
				TextView nameTextView = (TextView)findViewById(R.id.nameTextView);
				TextView contact = (TextView)findViewById(R.id.contactTextView);
				TextView email = (TextView)findViewById(R.id.emailTextView);
				ImageView fbImage = (ImageView)findViewById(R.id.fbImageView);
				nameTextView.setText(passenger.getName());
				contact.setText(passenger.getContact());
				email.setText(passenger.getEmail());
				fbImage.setImageBitmap(BitmapFactory.decodeFile(
					Environment.getExternalStorageDirectory()+"/"+FB_FRIEND_IMAGE_PATH));
				doneCount = 1;
			}
		}
		
	}
	
	private class GetPassengerAsyncTask extends AsyncTask<String, Void, Boolean> {
		protected ProgressDialog progressDialog;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(ShowPassengerActivity.this, "Loading Details", "Please wait...", true, false);
		}
		@Override
		protected Boolean doInBackground(String... params) {
			HashMap<String, String> data = new HashMap<String,String>();
			data.put("id", params[0]);
			passenger = (User)WebClient.postRequestForObject("get_user", data, WebClient.DECODE_OBJECT_TYPE_USER);
			
			
			
			if ( passenger != null ) {
				return true;
			}
			else return false;
		}
		@Override
		protected void onPostExecute(Boolean success) {
			super.onPostExecute(success);
			if ( success ) {
				progressDialog.dismiss();
			}
			else {
				Log.e("Web Service", "Could not retrive Driver data");
			}
		}
		
	}
	private class DownloadFbImageAsyncTask extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected Boolean doInBackground(Void... params) {
			File f = new File(Environment.getExternalStorageDirectory()+"/"+FB_FRIEND_IMAGE_PATH);
			if ( passenger != null ) {
				boolean decodeSuccess = WebClient.loadImageFromWebOperations(
					"http://graph.facebook.com/"
							+ passenger.getFbUrl().trim()
							+ "/picture", Environment.getExternalStorageDirectory()+"/"+FB_FRIEND_IMAGE_PATH );
				return decodeSuccess;
			}
			return false;
			
		}

	}
}
